<!--
 * @Descripttion: (表 4 矿井基本信息数据项说明/Pro_coalmine_info)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2024-12-18)
 * @LastEditors: (admin)
 * @LastEditTime: (2024-12-18)
-->
<template>
  <div class="app-container">
    <!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
    <el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" :label-width="labelWidth"
      v-show="showSearch" @submit.native.prevent>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" plain icon="el-icon-edit" size="mini"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" plain icon="el-icon-delete" size="mini"
          @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" border highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column prop="belongMine" label="煤矿编码" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="name" label="煤矿名称" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="mineType" label="煤矿分类" align="center">
        <template slot-scope="scope">
          <dict-tag :options="mineTypeOptions" :value="scope.row.mineType" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="矿井状态" align="center">
        <!-- <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status" />
        </template> -->
      </el-table-column>
      <el-table-column prop="type" label="煤矿类型" align="center">
        <template slot-scope="scope">
          <dict-tag :options="typeOptions" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column prop="recoveryType" label="开采类型" align="center">
        <template slot-scope="scope">
          <dict-tag :options="recoveryTypeOptions" :value="scope.row.recoveryType" />
        </template>
      </el-table-column>
      <el-table-column prop="designAbility" label="设计生产能力" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="checkAbili" label="核定生产能" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="licenseStatus" label="许可证状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="licenseStatusOptions" :value="scope.row.licenseStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="standardLevel" label="安全生产标准化等级" align="center" :show-overflow-tooltip="true" />
      <!-- <el-table-column prop="fileName" label="煤矿基本信息文档名称" align="center" :show-overflow-tooltip="true" /> -->

      <!-- <el-table-column prop="intelligentBuild" label="智能化建设情况" align="center" :show-overflow-tooltip="true" /> -->
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">
          <el-button size="mini" type="success" icon="el-icon-edit" title="编辑"
            @click="handleUpdate(scope.row)"></el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" title="删除"
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改表 4 矿井基本信息数据项说明对话框 -->
    <el-dialog :title="title" :lock-scroll="false" :visible.sync="open" width="800px">
      <el-form ref="form" :model="form" :rules="rules" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="12">
            <el-form-item label="煤矿编码" prop="belongMine">
              <el-input style="width: 220px;" v-model="form.belongMine" :disabled="true" placeholder="请输入煤矿编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="煤矿名称" prop="name">
              <el-input style="width: 220px;" v-model="form.name" :disabled="true" placeholder="请输入煤矿名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="煤矿地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入煤矿地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="煤矿分类" prop="mineType">
              <el-select style="width: 220px;" v-model="form.mineType" placeholder="请选择煤矿分类">
                <el-option v-for="item in mineTypeOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="煤矿类型" prop="type">
              <el-select style="width: 220px;" v-model="form.type" placeholder="请选择煤矿类型">
                <el-option v-for="item in typeOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="许可证状态" prop="licenseStatus">
              <el-select style="width: 220px;" v-model="form.licenseStatus" placeholder="请选择许可证状态">
                <el-option v-for="item in licenseStatusOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="矿井状态" prop="status">
              <el-input style="width: 220px;" v-model="form.status" placeholder="请输入矿井状态" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开采类型" prop="recoveryType">
              <el-select style="width: 220px;" v-model="form.recoveryType" placeholder="请选择开采类型">
                <el-option v-for="item in recoveryTypeOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安全生产标准化等级" prop="standardLevel">
              <el-select style="width: 220px;" v-model="form.standardLevel" placeholder="请选择开采类型">
                <el-option v-for="item in stateFlagOptions" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
              <!-- <el-input v-model="form.standardLevel" placeholder="请输入安全生产标准化等级" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="设计生产能力" prop="designAbility">
              <el-input v-model="form.designAbility" placeholder="请输入设计生产能力" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="核定生产能" prop="checkAbili">
              <el-input v-model="form.checkAbili" placeholder="请输入核定生产能" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="智能化建设情况" prop="intelligentBuild">
              <el-input v-model="form.intelligentBuild" placeholder="请输入智能化建设情况" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="煤矿基本信息" prop="fileName">
              <el-upload :file-list="form.fileNames" action="#" :limit="3" :on-exceed="handleExceed" :before-upload="(file) => {
      return handleBeforeUpload(file)
    }
      " accept=".jpg,.jpeg,.png,.pdf,.JPG,.JPEG,.PNG,.PDF">
                <el-button size="small" type="primary">上传</el-button>
                <div slot="tip" class="el-upload__tip">可选择文件上传，支持jpg、jpeg、png、pdf格式，单个文件最大不超过5MB</div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {
  listProCoalmineInfo,
  addProCoalmineInfo,
  delProCoalmineInfo,
  updateProCoalmineInfo,
  getProCoalmineInfo,
} from '@/api/project/proCoalmineInfo.js';

export default {
  name: "procoalmineinfo",
  data() {
    return {
      labelWidth: "100px",
      formLabelWidth: "160px",
      // 选中uuid数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: undefined,
        sortType: undefined,
      },
      // 弹出层标题
      title: "",
      // 操作类型 1、add 2、edit
      opertype: 0,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},

      //  sys_mkfl_type 煤矿分类选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      mineTypeOptions: [],
      // 矿井状态选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      statusOptions: [],
      // 煤矿类型选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      typeOptions: [],
      // 开采类型选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      recoveryTypeOptions: [],
      // 许可证状态选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      licenseStatusOptions: [],
      // 数据操作标识选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      stateFlagOptions: [],
      // 数据列表
      dataList: [],
      // 总记录数
      total: 0,
      // 提交按钮是否显示
      btnSubmitVisible: true,
      // 表单校验
      rules: {
        uuid: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        belongMine: [
          { required: true, message: "煤矿编码不能为空", trigger: "blur" }
        ],

        name: [
          { required: true, message: "煤矿名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "煤矿地址不能为空", trigger: "blur" }
        ],

        mineType: [
          { required: true, message: "煤矿分类不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "煤矿类型不能为空", trigger: "change" }
        ],
        licenseStatus: [
          { required: true, message: "许可证状态不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "矿井状态不能为空", trigger: "blur" }
        ],
        recoveryType: [
          { required: true, message: "开采类型不能为空", trigger: "change" }
        ],
        standardLevel: [
          { required: true, message: "安全生产标准化等级不能为空", trigger: "change" }
        ],
        designAbility: [
          { required: true, message: "设计生产能力不能为空", trigger: "blur" }
        ],

        checkAbili: [
          { required: true, message: "核定生产能不能为空", trigger: "blur" }
        ],

        intelligentBuild: [
          { required: true, message: "智能化建设情况不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    // 列表数据查询
    this.getList();
    this.getDicts('sys_mkfl_type').then((response) => {
      this.mineTypeOptions = response.data
    })

    this.getDicts('sys_mklx_type').then((response) => {
      this.typeOptions = response.data
    })


    this.getDicts('sys_kclx_type').then((response) => {
      this.recoveryTypeOptions = response.data
    })
    this.getDicts('sys_xkz_type').then((response) => {
      this.licenseStatusOptions = response.data
    })

    this.getDicts('sys_aqdj_type').then((response) => {
      this.stateFlagOptions = response.data
    })


  },
  methods: {
    handleExceed() {
      this.$message.error('上传文件不能超过3个')
    },
    handleViewFile(file) {
      let a = document.createElement('a')
      a.href = file.path
      a.download = file.name
      a.click()
    },
    onRemove(file, type) {
      if (type == '1') {
        let index = this.form.JsFiles.findIndex((fileItem) => fileItem.name === file.name)
        this.form.JsFiles.splice(index, 1)
        console.log(this.form.JsFiles)
      }
      if (type == '2') {
        let index = this.form.HtFiles.findIndex((fileItem) => fileItem.name === file.name)
        this.form.HtFiles.splice(index, 1)
        console.log(this.form.HtFiles)
      }
    },
    handleBeforeUpload(file) {
      this.uploadFile(file)
      return false
    },
    //图片上传
    uploadFile(file) {
      debugger;
      var _this = this
      if (file.size >= 50 * 1024 * 1024) {
        this.$message.error('上传文件大小不能超过50MB')
        return false
      }
      var num = file.name.match(/[.]/g).length
      const extension1 = file.name.split('.')[num].toLowerCase() === 'jpg'
      const extension2 = file.name.split('.')[num].toLowerCase() === 'png'
      const extension4 = file.name.split('.')[num].toLowerCase() === 'jpeg'
      const extension5 = file.name.split('.')[num].toLowerCase() === 'JPG'
      const extension6 = file.name.split('.')[num].toLowerCase() === 'JPEG'
      const extension7 = file.name.split('.')[num].toLowerCase() === 'PNG'
      const extension8 = file.name.split('.')[num].toLowerCase() === 'pdf'
      const extension9 = file.name.split('.')[num].toLowerCase() === 'PDF'
      const extension10 = file.name.split('.')[num].toLowerCase() === 'DOCX'
      const extension11 = file.name.split('.')[num].toLowerCase() === 'docx'
      if (!extension10 && !extension10 && !extension1 && !extension2 && !extension4 && !extension5 && !extension6 && !extension8 && !extension9 && !extension7) {
        this.$message.error('请上传正确格式，支持jpg、jpeg、png、pdf、docx格式。')
        return false
      }
      var formdata = new FormData()
      formdata.append('file', file)
      formdata.append('showUrl', this.setting.fileurl)
      formdata.append('path', '')
      formdata.append('scene', 'default')
      var settings = {
        url: this.setting.fileurl + '/group1/upload',
        method: 'POST',
        timeout: 0,
        processData: false,
        mimeType: 'multipart/form-data',
        contentType: false,
        data: formdata,
      }
      $.ajax(settings).done(function (response) {
        if (response != null) {
          if (file.name.length > 36) {
            _this.$message.error('文件名称不能超过36个字符！')
            return false
          }
          _this.form.fileUrl = response;
          _this.form.fileName = file.name
          _this.form.fileUrls.push({
            name: file.name,
            Path: response,
            url: response,
          })
        } else {
          // _this.$message.error("文件上传失败");
        }
      })
    },
    // 查询数据
    getList() {
      this.loading = true;
      listProCoalmineInfo(this.queryParams).then(res => {
        if (res.code == 200) {
          this.dataList = res.data.result;
          this.total = res.data.totalNum;
          this.loading = false;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置数据表单
    reset() {
      this.form = {
        uuid: undefined,
        belongMine: undefined,
        name: undefined,
        address: undefined,
        mineType: undefined,
        status: undefined,
        type: undefined,
        recoveryType: undefined,
        designAbility: undefined,
        checkAbili: undefined,
        licenseStatus: undefined,
        standardLevel: undefined,
        fileName: undefined,
        stateFlag: undefined,
        intelligentBuild: undefined,
        createDate: undefined,
        updateDate: undefined,
        id: undefined,
      };
      this.resetForm("form");
    },
    // 重置查询操作
    resetQuery() {
      this.timeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1
      this.multiple = !selection.length;
    },
    // 自定义排序
    sortChange(column) {
      if (column.prop == null || column.order == null) {
        this.queryParams.sort = undefined;
        this.queryParams.sortType = undefined;
      } else {
        this.queryParams.sort = column.prop;
        this.queryParams.sortType = column.order;
      }

      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getbelongMine().then(res => {
        if (res.code == 210) {
          this.$message({
            message: '请先在企业信息管理中添加煤矿编码',
            type: 'warning'
          });
        } else {
          this.reset();
          this.form.belongMine = res.data.sysCode;
          this.form.name = res.data.sysDw;
          this.open = true;
          this.title = "添加";
          this.opertype = 1;

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids;

      this.$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
        .then(function () {
          return delProCoalmineInfo(Ids);
        })
        .then(() => {
          this.handleQuery();
          this.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProCoalmineInfo(id).then((res) => {
        const { code, data } = res;
        if (code == 200) {
          this.open = true;
          this.title = "修改数据";
          this.opertype = 2;

          this.form = {
            ...data,
          };
        }
      });
    },
    // 煤矿分类字典翻译
    mineTypeFormat(row, column) {
      return this.selectDictLabel(this.mineTypeOptions, row.mineType);
    },
    // 矿井状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 煤矿类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 开采类型字典翻译
    recoveryTypeFormat(row, column) {
      return this.selectDictLabel(this.recoveryTypeOptions, row.recoveryType);
    },
    // 许可证状态字典翻译
    licenseStatusFormat(row, column) {
      return this.selectDictLabel(this.licenseStatusOptions, row.licenseStatus);
    },
    // 数据操作标识字典翻译
    stateFlagFormat(row, column) {
      return this.selectDictLabel(this.stateFlagOptions, row.stateFlag);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(JSON.stringify(this.form));

          if (this.form.uuid != undefined && this.opertype === 2) {
            updateProCoalmineInfo(this.form)
              .then((res) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
              });
          } else {
            addProCoalmineInfo(this.form)
              .then((res) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((err) => {
                //TODO 错误逻辑
              });
          }
        }
      });
    },
  },
};
</script>